﻿using System.Collections.Generic;
using Client.Interfaces;
using Client.UserControls;
using Global.Interfaces.Server;
using IncomingChatObserver;
using Client.Chat;
using System;
using Global.Impl.Server;

namespace Client.Controllers
{
    internal class MainController : IMainController
    {
        #region Variables
        private IMainView _view;
        private ChatObserver _chatObserver;
        private string _username;
        private string _password;
        #endregion

        #region Constructor
        public MainController(IMainView form)
        {
            _view = form;
            _chatObserver = new ChatObserver();
            _chatObserver.ChatObserved += new ChatEventHandler(_chatObserver_ChatObserved);

            ChatSessions = new List<ChatSession>();
        }

        void _chatObserver_ChatObserved(object sender, ChatEventArgs e)
        {
            if (ShouldIJoinChat(e.Guid))
            {
                ChatWindow chatWindow = new ChatWindow(this, e.Guid, e.Password);
                chatWindow.Show();
            }
        }
        #endregion

        #region IMainController Members

        public List<ChatSession> ChatSessions { get; set; }

        public string Username
        {
            get { return _username; }
        }

        public string Password
        {
            get { return _password; }
        }

        public void Start()
        {
            _view.Panel.Controls.Clear();
            Login login = new Login(this);
            login.Dock = System.Windows.Forms.DockStyle.Fill;
            _view.Panel.Controls.Add(login);
            login.Focus();
        }

        public void LoginSucces(string username, string password)
        {
            this._username = username;
            this._password = password;

            _view.Panel.Controls.Clear();
            _chatObserver.Start();

            BuddyList buddylist = new BuddyList(this);
            buddylist.Dock = System.Windows.Forms.DockStyle.Fill;
            _view.Panel.Controls.Add(buddylist);
        }

        public void Close()
        {
            this.Dispose();
        }

        private bool ShouldIJoinChat(string _endpoint)
        {
            foreach (ChatSession session in ChatSessions)
            {
                if (session.Guid == _endpoint)
                    return false;
            }
            ChatSessions.Add(new ChatSession(_endpoint));
            return true;
        }

        public void LeftChat(string endpoint)
        {
            ChatSession chatSession = null;
            foreach (var session in ChatSessions)
            {
                if (session.Guid == endpoint)
                {
                    chatSession = session;
                    break;
                }
            }
            if (chatSession != null)
                ChatSessions.Remove(chatSession);
        }

        public void CreateChatSession(ChatRequest request)
        {
            if (ShouldIJoinChat(request.Endpoint))
                _view.OpenNewChatWindows(request);
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (_chatObserver != null)
                _chatObserver.Dispose();
        }

        #endregion
    }
}